/**
*  \file secacc.h
*
*  \brief   Security Access
*
*  \author  Michael J. Hohmann <michael.hohmann@linde-mh.de>
*
*
*/

#ifndef SECACC_H_INCLUDED
#define SECACC_H_INCLUDED

#ifndef SECACC_LINKAGE
#define SECACC_LINKAGE extern
#endif

/*================================================[ public includes  ]================================================*/
#include "../cfg/prj.h"
#include "../kwp/kwp.h"

/*================================================[ public defines   ]================================================*/

/*================================================[ public datatypes ]================================================*/

/** Security States */
enum secacc_state
{
   secacc_state_default       =  0x01,  /**< No security level          */
   secacc_state_programming   =  0x02,  /**< Programming State          */
   secacc_state_service       =  0x04,  /**< Field Flash                */
   secacc_state_eopl          =  0x08   /**< End Of Production Line     */
};

/*================================================[ public variables ]================================================*/

/*================================================[ public functions ]================================================*/
/** Initialization.
*
*  Initialize the secacc module.
*
*  \return Always RC_SUCCESS.
*/
RC secacc_initialize( void );

/** Get Security State
*
*  The function secacc_get_state() will return the current security state.
*
*  \return security state
*/
enum secacc_state secacc_get_state( void );

/** Set Security State
*
*  The function secacc_set_state() set the security state to a new value
*
*  \param state new security state
*/
void secacc_set_state( enum secacc_state state );

/** Reset Security State
*
*  The function  secacc_reset_state() will set the security state to secacc_state_default.
*  This operation should be done if a KWP session is started.
*/
void secacc_reset_state( void );


/** Process a Request
*
*  The function secacc_process_request() process incoming KWP requests with SID 0x27.
*
*  Subfunctions 1 to 6 are allowed, all other values will return KWP_RESPONSE_CODE_SUBFUNCTIONNOTSUPPORTED.
*
*  The sub function 1,3 and 5 have a length of 1 byte. Any other length will cause the return code
*  KWP_RESPONSE_CODE_INVALIDFORMAT. If the Security state is in state secacc_state_access, the function will
*  return with KWP_RESPONSE_CODE_CONDITIONSNOTCORRECT.
*
*  If the security state is already in the destination state, ( secacc_state_programming for subfunction 1,
*  secacc_state_service for subfunction 2 or secacc_state_eopl for subfunction 5) the function
*  returns KWP_RESPONSE_CODE_NONE, and a valid message.
*  If the security state is in any other state the function will return KWP_RESPONSE_CODE_NONE and a message
*  with a new seed, the security state will be set to secacc_state_access.
*
*  For messages with subfunctions 2,4 or 6  a length of 5 bytes is expected, any other values
*  will cause KWP_RESPONSE_CODE_INVALIDFORMAT.
*  If the security state is unequal to secacc_state_access the functions returns KWP_RESPONSE_CODE_REQUESTSEQUENCEERROR.
*  If the received key is correct, the security state is switched to the according state (
*  secacc_state_programming for subfunction 2, secacc_state_service for subfunction 4 or secacc_state_eopl
*  subfunction for 6.
*
*  Can Trace
*
*  4.334684 1  781            Rx   d 8 02 27 01 00 00 00 00 00
*  4.335164 1  791            Rx   d 8 06 67 01 5F E3 AD 94 00
*  4.335678 1  781            Rx   d 8 06 27 02 D0 A4 5A D2 00
*  4.336162 1  791            Rx   d 8 02 67 02 5F E3 AD 94 00
*
*
*  3.839437 1  781            Rx   d 8 02 27 03 00 00 00 00 00
*  3.840189 1  791            Rx   d 8 06 67 03 75 4D 51 92 00
*  3.840689 1  781            Rx   d 8 06 27 04 6C 68 51 92 00
*  3.841187 1  791            Rx   d 8 02 67 04 75 4D 51 92 00
*
*
*  37.3443 1  781             Rx   d 8 02 27 05 FF FF FF FF FF
*  37.3467 1  791             Rx   d 8 06 67 05 00 00 3C AF 30
*  37.3577 1  781             Rx   d 8 06 27 06 46 64 AA 55 FF
*  37.3669 1  791             Rx   d 8 02 67 06 00 00 3C AF 30
*
*  \param condition Additional conditions. E.g passive mode.
*  \param dat Pointer to the message buffer.
*  \param len Length of the message.
*
*  \return NRC_NONE if a response is generated otherwise an error is occurred.
*/
enum kwp_response_code secacc_process_request( BO condition, U8  *dat, U16 *len);

/*====================================================================================================================*/
#endif

/***********************************************************************************************************************
*                             Copyright 2010 Linde Material Handling. All rights reserved.                             *
***********************************************************************************************************************/
